/**
 *
 */
package com.ybkj.daijia.api.member.v3;

import com.ybkj.daijia.api.common.AesUtil;
import com.ybkj.daijia.api.member.ErrorConstant;
import com.ybkj.daijia.api.member.SecurityUtils;
import com.ybkj.daijia.api.member.transport.ResultTransport;
import com.ybkj.daijia.common.Result;
import com.ybkj.daijia.server.mc.Passenger;
import com.ybkj.daijia.server.service.MemberAccountService;
import com.ybkj.daijia.server.service.MemberService;
import java.util.LinkedList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 司机设备
 * @author bo
 *
 */
@Controller("MemberDeviceV3Controller")
@RequestMapping(value = "member/api/rest/v3")
public class DeviceController {

    private static final Logger logger = LoggerFactory.getLogger(DeviceController.class);

    @Autowired
    private MemberService memberService;

    @Autowired
    private MemberAccountService memberAccountService;


    @RequestMapping(value = "bindDevice", method = RequestMethod.POST)
    public @ResponseBody
    ResultTransport bindDevice(String phone, String deviceType, String cid, String appKey,
        String token, String timestamp) {

        logger.debug(
            "recv member bindDevice -- phone:{},deviceType:{},cid:{},appKey:{},token:{},timestamp:{}",
            phone, deviceType, cid, appKey, token, timestamp);

        if (StringUtils.isBlank(phone) || StringUtils.isBlank(deviceType) || StringUtils
            .isBlank(cid) || SecurityUtils.isBlank(appKey, token, timestamp)) {
            return ResultTransport.getErrorBy(ErrorConstant.PARAM_ERROR);
        }

//		if(!StringUtils.isNumeric(timestamp) || !SecurityUtils.checkTimestamp(Long.parseLong(timestamp))) {
//			return ResultTransport.getErrorBy(ErrorConstant.TIMESTAMP_ERROR);
//		}

        List<String> params = new LinkedList<String>();
        params.add(phone);
        params.add(deviceType);
        params.add(cid);
        params.add(timestamp);
        params.add(appKey);

        if (!SecurityUtils.checkToken(params, token)) {
            return ResultTransport.getErrorBy(ErrorConstant.TOKEN_ERROR);
        }

        Passenger passenger = memberService.findOnePassengerByPhone(phone);

        if (null == passenger) {
            return ResultTransport.getErrorBy(ErrorConstant.MEMBER_NOT_EXISTS_ERROR);
        }

        try {

            passenger.setDeviceType(deviceType);
            passenger.setCid(cid);
            Result<Passenger> result = memberService.bindDevice(passenger);
            if (result.isSuccess()) {
                ResultTransport instance = ResultTransport.getErrorBy(ErrorConstant.SUCCESS);
                return instance;
            } else {
                ResultTransport instance = ResultTransport.getErrorBy(ErrorConstant.SERVER_ERROR);
                return instance;
            }

        } catch (Exception e) {
            logger.error("api v3 member bindDevice error: {}", e);
            return ResultTransport.getErrorBy(ErrorConstant.SERVER_ERROR);
        }

    }

    @RequestMapping(value = "bindDevice2", method = RequestMethod.POST)
    public @ResponseBody
    ResultTransport bindDevice2(String phone, String deviceType, String cid, String appKey,
        String token, String timestamp) {

        logger.debug(
            "recv member bindDevice -- phone:{},deviceType:{},cid:{},appKey:{},token:{},timestamp:{}",
            phone, deviceType, cid, appKey, token, timestamp);
        if (StringUtils.isBlank(phone) || StringUtils.isBlank(deviceType) || StringUtils
            .isBlank(cid) || SecurityUtils.isBlank(appKey, token, timestamp)) {
            return ResultTransport.getErrorBy(ErrorConstant.PARAM_ERROR);
        }

        phone = AesUtil.aesDecrypt(String.valueOf(phone).replaceAll(" ", "+"), AesUtil.VIPARA);

//		if(!StringUtils.isNumeric(timestamp) || !SecurityUtils.checkTimestamp(Long.parseLong(timestamp))) {
//			return ResultTransport.getErrorBy(ErrorConstant.TIMESTAMP_ERROR);
//		}

        List<String> params = new LinkedList<String>();
        params.add(phone);
        params.add(deviceType);
        params.add(cid);
        params.add(timestamp);
        params.add(appKey);

        if (!SecurityUtils.checkToken(params, token)) {
            return ResultTransport.getErrorBy(ErrorConstant.TOKEN_ERROR);
        }

        Passenger passenger = memberService.findOnePassengerByPhone(phone);

        if (null == passenger) {
            return ResultTransport.getErrorBy(ErrorConstant.MEMBER_NOT_EXISTS_ERROR);
        }

        try {
            passenger.setDeviceType(deviceType);
            passenger.setCid(cid);
            Result<Passenger> result = memberService.bindDevice(passenger);
            if (result.isSuccess()) {
                ResultTransport instance = ResultTransport.getErrorBy(ErrorConstant.SUCCESS);
                return instance;
            } else {
                ResultTransport instance = ResultTransport.getErrorBy(ErrorConstant.SERVER_ERROR);
                return instance;
            }

        } catch (Exception e) {
            logger.error("api v3 member bindDevice error: {}", e);
            return ResultTransport.getErrorBy(ErrorConstant.SERVER_ERROR);
        }

    }

}
